package com.kool.system.action.param;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.system.bean.SyParamBean;
import com.kool.system.bean.io.param.SYPAMINFYBean;
import com.kool.system.service.ParamService;

/**
 * @DESCRIBE 查询
 * @AUTHOR LUYU
 */
@Controller
public class QueryParamAction implements IBaseAction {
	@Autowired
	private ParamService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/sys/param/queryParam", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("SYPAMINFY");
		SyParamBean beanIn = null;
		if (secIn != null && secIn.size() > 0) {
			JSONObject propIn = secIn.getJSONObject(0);
			SYPAMINFYBean ioIn = JSON.toJavaObject(propIn, SYPAMINFYBean.class);
			beanIn = (SyParamBean) BeanUtils.io2Bean(ioIn, SyParamBean.class, "spa");
		} else {
			beanIn = new SyParamBean();
		}

		List<SyParamBean> list = service.query(beanIn);

		JSONObject pkgOut = new JSONObject();
		JSONArray secOut = new JSONArray();
		for (SyParamBean syParam : list) {
			SYPAMINFYBean ioOut = (SYPAMINFYBean) BeanUtils.bean2Io(syParam, SYPAMINFYBean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			secOut.add(propOut);
		}
		pkgOut.put("SYPAMINFY", secOut);
		return pkgOut;
	}
}
